package database;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insert extends JInternalFrame {
	
	Connection conn;
	
	private JLabel lblNewLabel;
	private JLabel lblEa;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	
	
	public Insert(Connection c) {
		super("삽입", true, true, true, true);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);  // 창을 끄고 메인에서 다시 선택했을때 뜨게하는것
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblEa());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		
		this.conn = c;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(37, 40, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblEa() {
		if (lblEa == null) {
			lblEa = new JLabel("ea");
			lblEa.setBounds(37, 65, 57, 15);
		}
		return lblEa;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(120, 37, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(120, 65, 116, 21);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						String c = code.getText();
						int e1 = Integer.parseInt(ea.getText());
						
						String sql = "insert into ptbl(serial, code, ea) values(PTble_serial.nextval, ?, ?)";
						PreparedStatement pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e1);
						
						pstmt.executeUpdate();
						
					}catch(Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(85, 124, 97, 23);
			
		}
		return btnNewButton;
	}
}
